package com.cn.shuangzi.userinfo.activity;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cn.shuangzi.SZApp;
import com.cn.shuangzi.SZBaseActivity;
import com.cn.shuangzi.activity.SZInterfaceActivity;
import com.cn.shuangzi.bean.VersionInfo;
import com.cn.shuangzi.common.VersionUpdate;
import com.cn.shuangzi.retrofit.SZRetrofitManager;
import com.cn.shuangzi.retrofit.SZRetrofitResponseListener;
import com.cn.shuangzi.userinfo.R;
import com.cn.shuangzi.util.SZFileUtil;
import com.cn.shuangzi.util.SZToast;
import com.cn.shuangzi.util.SZUtil;
import com.cn.shuangzi.view.sweet_alert.SweetAlertDialog;
import com.google.gson.Gson;

/**
 * Created by CN on 2018-1-25.
 */

public abstract class SZSettingActivity extends SZBaseActivity implements SZInterfaceActivity {
    private Button btnLogout;
    private TextView txtVersion;
    private TextView txtCache;
    private View rltClearCache;
    private View lltAccountSafe;
    private View rltUpdate;

    @Override
    protected int onGetChildView() {
        return R.layout.activity_setting;
    }

    @Override
    protected void onBindChildViews() {
        btnLogout = findViewById(R.id.btnLogout);
        txtCache = findViewById(R.id.txtCache);
        txtVersion = findViewById(R.id.txtVersion);
        rltClearCache = findViewById(R.id.rltClearCache);
        lltAccountSafe = findViewById(R.id.lltAccountSafe);
        rltUpdate = findViewById(R.id.rltUpdate);
    }

    @Override
    protected void onBindChildListeners() {
        rltClearCache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clearCache();
            }
        });
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
            }
        });
        lltAccountSafe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(isUserLogin() ? getAccountSafeClass() : getLoginClass());
            }
        });
        rltUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestNewVersion();
            }
        });
    }

    @Override
    protected void onChildViewCreated() {
        onChildViewCreatedPre();
        setTitleTxt(R.string.txt_setting);
        showBackImgLeft(getBackImgLeft());
        if (!isUserLogin()) {
            btnLogout.setVisibility(View.GONE);
        }
        lltAccountSafe.setVisibility(isShowAccountSafe() ? View.VISIBLE : View.GONE);
        txtCache.setText(SZFileUtil.getInstance().getAllCacheFileSize());
        requestNewVersionOnly();
    }

    @Override
    protected void onReloadData(boolean isRefresh) {

    }

    private void requestNewVersion() {
        showBar();
        new VersionUpdate(this, 7, new VersionUpdate.VersionUpdateListener() {
            @Override
            public void onSuccess(VersionInfo versionInfo, String data) {
                closeBar();
            }

            @Override
            public void onNetError(int errorCode, String errorMsg) {
                closeBar();
                if (errorCode == SZRetrofitManager.ERROR_EXCEPTION || errorCode == SZRetrofitManager.ERROR_NET_FAILED) {
                    SZToast.error("网络异常!");
                } else {
                    SZToast.error(errorMsg);
                }
            }

            @Override
            public void onUpdateAlertDismiss(VersionInfo versionInfo) {
            }

            @Override
            public void onIgnoreUpdateAlertShow(VersionInfo versionInfo, String data) {
            }
        }).checkNewVersion(true);
    }

    private void requestNewVersionOnly() {
        request(SZRetrofitManager.getInstance().getSZRequest().getNewVersionInfo(SZApp.getInstance().getSZAppId(), "ANDROID", SZUtil.getVersionCode())
                , new SZRetrofitResponseListener.SimpleSZRetrofitResponseListener() {
                    @Override
                    public void onSuccess(String data) {
                        VersionInfo versionInfo = new Gson().fromJson(data, VersionInfo.class);
                        if (versionInfo.isLastVersion()) {
                            txtVersion.setText("有新版本");
                        }
                    }

                    @Override
                    public void onNetError(int errorCode, String errorMsg) {
                    }
                });
    }

    private void clearCache() {
        showWarningAlert(getString(R.string.txt_clear_cache), "确认清空所有缓存的图片和文件吗？", new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismissWithAnimation();
                SZFileUtil.getInstance().clearAllCacheFile();
                txtCache.setText(SZFileUtil.getInstance().getAllCacheFileSize());
            }
        });
    }

    private void logout() {
        showWarningAlert("退出登录", "确认退出登录吗？", new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismissWithAnimation();
                btnLogout.setVisibility(View.GONE);
                onLogout();
            }
        });
    }

    public abstract boolean isUserLogin();

    public abstract boolean isShowAccountSafe();

    public abstract void onLogout();

    public abstract Class getLoginClass();

    public abstract Class getAccountSafeClass();

}
